package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.MerchantConfig;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IMerchantAppService;
import com.qianniu.napi.admin.service.IMerchantConfigService;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * <p>
 * 商家配置
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-31
 */
@Controller
public class MerchantConfigController {

    private static final Logger logger = LoggerFactory.getLogger(MerchantConfigController.class);
    //
    @Autowired
    private IMerchantAppService merchantAppService;
    @Autowired
    private IMerchantConfigService merchantConfigService;
    @Autowired
    private IRedisService redisService;

    @RequestMapping("admin/merchantconfig/index")
    public String index() {
        return "admin/merchantconfig";
    }

    @RequestMapping("/admin/merchantconfig/list")
    @ResponseBody
    public Page<MerchantConfig> list(@RequestParam(value = "limit", required = false) int limit,
                                     @RequestParam(value = "order", required = false) String order,
                                     @RequestParam(value = "offset", required = false) int offset) {
        User user = ObjectUtil.getSessionUser();
        Wrapper<MerchantConfig> wrapper = new EntityWrapper<MerchantConfig>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());

        if (!Strings.isNullOrEmpty(order)) wrapper.orderBy("id", order.equalsIgnoreCase("asc"));

        Page<MerchantConfig> page = merchantConfigService.selectPage(new Page<MerchantConfig>(offset / limit + 1, limit), wrapper);
        for(MerchantConfig mconfig:page.getRecords()){
            mconfig.setAppname(redisService.getMApp(mconfig.getMpid()).getName());
        }

        return page;
    }

    @RequestMapping("/admin/merchantconfig/add")
    public String add(MerchantConfig bean) {
        // 字段校验
//        if( bindingResult.hasErrors())
//        {
//            logger.debug( "There are errors! {}", bindingResult );
//            return null;
//        }
        logger.debug(">>>merchantconfig/add:" + JSON.toJSONString(bean));

        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/merchantconfig";
    }

    @RequestMapping("/admin/merchantconfig/getMerchantConfigById")
    @ResponseBody
    public MerchantConfig getMerchantConfigById(long id) {
        MerchantConfig merchantconfig = merchantConfigService.selectById(id);
        return merchantconfig;
    }

    @RequestMapping("/admin/merchantconfig/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = merchantConfigService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/merchantconfig/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        MerchantConfig bean = new MerchantConfig();
        bean.setId(id);
//        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        merchantConfigService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/merchantconfig/modify")
    public String update(MerchantConfig bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        logger.debug(">>>merchantconfig/modify:" + JSON.toJSONString(bean));
        boolean isSuccc = merchantConfigService.updateById(bean);
        if (isSuccc) {
            Long mpid = ObjectUtil.getMpid();
            String rkey = Constants.REDIS_MERCHANT_CONFIG + mpid;
            redisService.del(rkey);
            return "redirect:index";
        }
        return "error";
    }
}
